package com.zyk.java8;

import java.util.Arrays;
import java.util.List;

/**
 * 学习以下jdk8的新特性
 *
 * @author zhangsan
 * @date 2021/4/2 11:30
 */
public class Main {

    public static void main(String[] args) {

        /*int aaaa = -1;
        System.out.println(Integer.toBinaryString(aaaa));
        System.out.println(Integer.toBinaryString(aaaa >> 5));  // 拿最高位. 不会改变正负数
        System.out.println(Integer.toBinaryString(aaaa >>> 5)); // 拿0   会改变正负数*/

        // jdk11的类型推断
        /*var a = 1;
        System.out.println(a);
        a += 1.1;
        System.out.println(a);

        var list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        System.out.println(list);*/


        // 接口定义默认实现: 模板模式.
        System.out.println("=====================接口默认实现===================");
        Get g = new Get() {
            @Override
            public void get() {
                before();
                System.out.println("get...");
                after();
            }

            /** 选择性重写 */
            /*@Override
            public void after() {
                System.out.println("修改after...");
            }*/
        };
        g.get();
        // lambda表达式
        System.out.println("=====================lambda表达式===================");
        List<String> names = Arrays.asList("peter", "anna", "mike", "xenia");
        System.out.println(names);

        names.sort((String::compareTo));

        System.out.println(names);

        List<Integer> ints = Arrays.asList(1, 1, 321, 5, 51, 135, 11, 316, 531, 3213, 2, 3, 4);
        boolean allMatch = ints.stream().allMatch((a) -> a > 0);
        System.out.println(allMatch);
        ints.stream().sorted((a, b) -> a - b).forEach((num) -> {
            System.out.print(num + " ");
        });
        System.out.println();
    }

}

interface Get {

    default void before() {
        System.out.println("before...");
    }

    void get();

    default void after() {
        System.out.println("after");
    }
}
